﻿using AnlianCommon.Common.Domains.SheepManagement;
using AnlianCommon.Common.DTO;
using AnlianCommon.Common.Infrastructure.IRepository;
using AnlianCommon.Enum;
using MediatR;
using Microsoft.EntityFrameworkCore;

namespace SheepCommon.API.SheepCommands.SheepCullinges
{
    public class SheepCullingQueryHandler : IRequestHandler<SheepCullingQueryCommand, ResultPage<SheepCulling>>
    {
        private readonly IBaseRepository<SheepCulling> _sheepCullingRepository;

        public SheepCullingQueryHandler(IBaseRepository<SheepCulling> sheepCullingRepository)
        {
            _sheepCullingRepository = sheepCullingRepository;
        }
        /// <summary>
        /// 业务逻辑
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<ResultPage<SheepCulling>> Handle(SheepCullingQueryCommand request, CancellationToken cancellationToken)
        {
            var result = new ResultPage<SheepCulling>() { Code = (int)CommonErrorCode.Search, TotalCount = 0, PagedCount = 0 };
            try
            {
                var sheepCulling = await (from a in await _sheepCullingRepository.GetAll()
                                          select new SheepCulling()
                                          {
                                              SheepCullingId = a.SheepCullingId,
                                              EliminationDate = a.EliminationDate,
                                              SheepNumber = a.SheepNumber,
                                              EliminationReasons = a.EliminationReasons,
                                              IsDel = a.IsDel,
                                              TreatmentMeasures = a.TreatmentMeasures,
                                              Variety = a.Variety,
                                              SheepSex = a.SheepSex,
                                              ReproductiveState = a.ReproductiveState,
                                              EliminationMonth = a.EliminationMonth,
                                              EliminationDay = a.EliminationDay,
                                              EliminationHouse = a.EliminationHouse,
                                              EliminationField = a.EliminationField,
                                              CreatedOwnerId = a.CreatedOwnerId,
                                              CreatedTime = a.CreatedTime,
                                          }).ToListAsync();
                if (request.StratEliminationDate != null)
                {
                    sheepCulling = sheepCulling.Where(c => c.EliminationDate >= request.StratEliminationDate).ToList();
                }
                if (request.EndEliminationDate != null)
                {
                    sheepCulling = sheepCulling.Where(c => c.EliminationDate <= request.EndEliminationDate).ToList();
                }
                if (!string.IsNullOrEmpty(request.SheepNumber))
                {
                    sheepCulling = sheepCulling.Where(c => c.SheepNumber == request.SheepNumber).ToList();
                }
                if (!string.IsNullOrEmpty(request.EliminationReasons))
                {
                    sheepCulling = sheepCulling.Where(c => c.EliminationReasons == request.EliminationReasons).ToList();
                }
                if (request.IsDel != null)
                {
                    sheepCulling = sheepCulling.Where(c => c.IsDel == request.IsDel).ToList();
                }
                result.TotalCount = sheepCulling.Count;
                result.PagedCount = (int)Math.Ceiling((result.TotalCount * 1.0) / request.PageSize);
                sheepCulling = sheepCulling.OrderBy(c => c.SheepCullingId).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize).ToList();
                result.Data = sheepCulling;
            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }
    }
}
